/*
 * @lc app=leetcode.cn id=22 lang=javascript
 *
 * [22] 括号生成
 */

// @lc code=start
/**
 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function (n) {
  let arr = [];
  var dfs = function (left, right, str) {
    if (str.length === 2 * n) {
      arr.push(str);
    }
    if (left > 0) {
      dfs(left - 1, right, `${str}(`);
    }
    if (right > left) {
      dfs(left, right - 1, `${str})`);
    }
  }
  dfs(n, n, '');
  return arr;
};
// @lc code=end

